﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Web.Mvc;
//using www.SService;
//using www.SService;

namespace www.Controllers
{
    public class AdminController : Controller
    {
        //private readonly ObjectCache _cache = MemoryCache.Default;
        //private readonly ServiceClient _service1 = new ServiceClient();
        //private readonly Utility _utility=new Utility();
        ////
        //// GET: /Admin/
        //[HttpGet]
        //public ActionResult Index()
        //{
        //    if (Session["AdminSuccess"] == null || Convert.ToBoolean(Session["AdminSuccess"]) == false)
        //        return RedirectToAction("Index", "Home");
        //    if (_cache["Menu"] == null)
        //    {
        //        var lData = _service1.GetMenuDataContracts();
        //        if (lData.Any())
        //        {
        //            var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(1) };
        //            _cache.Set("Menu", lData, policy);

        //        }
        //    }
        //   if (_cache["StaffMenu"] == null)
        //    {
        //        var lData = _service1.GetMenuMemberDataContracts();
        //        if (lData.Any())
        //        {
        //            var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(1) };
        //            _cache.Set("StaffMenu", lData, policy);
        //        }
        //    }
        //    var lMenu = (List<RelaxMenuDataContract>) _cache["Menu"];
        //    var lStaffMenu = (List<RelaxMenuMemberDataContract>) _cache["StaffMenu"];
        //    if (lMenu == null || lStaffMenu == null)
        //        return View();
        //    var permistion = (from m in lMenu.Where(p=> p.Active==1 && p.Parent=="0")
        //        //join s in lStaffMenu.Where(p => p.MemberId == Convert.ToInt32(Session["StaffId"]) && p.Active==1) on m.Id equals
        //        //    s.MenuId
        //        select new RelaxMenuDataContract
        //        {
        //            Id=m.Id,
        //            Name = m.Name,
        //            Tag = m.Tag,
        //            Keyword = m.Keyword,
        //            Decription = m.Decription,
        //            //Level = m.Level,
        //            Parent = m.Parent,
        //            //Type = m.Type,
        //            Link = m.Link,
        //            Target = m.Target,
        //            Ord = m.Ord,
        //            Active = m.Active
        //        }).ToList();

        //    Session["MenuAdmin"] = permistion;

        //    return View();
        //}

        //[HttpGet]
        //public ActionResult Logon()
        //{
        //    if (_cache["User"] == null)
        //    {
        //        var lData = _service1.GetAllUser();
        //        if (lData.Any())
        //        {
        //            var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
        //            _cache.Set("User", lData, policy);
        //        }
        //    }
        //    return View();
        //}

        //[HttpPost]
        //public ActionResult Logon(FormCollection collection)
        //{
        //    if (string.IsNullOrEmpty(collection["username"]))
        //        return View();
        //    if (string.IsNullOrEmpty(collection["password"]))
        //        return View();
        //    var lData = (List<RelaxLoginsDataContract>)_cache["User"];
        //    if (lData == null)
        //        return View();
        //    var first = lData.FirstOrDefault(p => p.UserName == collection["username"]);
        //    if (first == null)
        //    {
        //        Response.Write("<script>alert('Tài khoản không tồn tại.');</script>");
        //        return View();
        //    }
        //    var pass = _utility.Decrypt("Dragon_hy",first.Password);
        //    if (pass == collection["password"])
        //    {
        //        Session["AdminSuccess"] = true;
        //        Session["AdminUser"] = first.UserName;
        //        Session["StaffId"] = first.StaffId;
        //        Response.Write("<script>alert('Xin chúc mừng,"+first.UserName+" bạn đã đăng nhập thành công.');</script>");
        //        return RedirectToAction("Index", "Admin");
        //    }
            
        //    return View();
        //}

        //[HttpGet]
        //public ActionResult Logout()
        //{
        //    Session["AdminSuccess"] = false;
        //    Session["AdminUser"] = string.Empty;
        //    Session["StaffId"] = string.Empty;
        //    Session["MenuAdmin"] = null;

        //    return RedirectToAction("Index", "Home");
        //}
    }
}
